﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlantsVsZombies
{
   
    public abstract class AttackPlant : Plant
    {
        
        protected int nBullet;
        
        protected int _bulletPerTime;
        protected float _velocity;
        protected double _loadBulletTime; // thoi gian dan cach giua moi lan ban.
        

        public int NBullet
        {
            get { return nBullet; }
            set { nBullet = value; }
        }

       

        public int BulletPerTime
        {
            get { return _bulletPerTime; }
            set { _bulletPerTime = value; }
        }

        public float Velocity
        {
            get { return _velocity; }
            set { _velocity = value; }
        }

       

        public double LoadBulletTime
        {
            get { return _loadBulletTime; }
            set { _loadBulletTime = value; }
        }

       

        public override void Shoot(Ghost ghost, GameTime gameTime)
        {
            
            // thang hang voi ma.
            if (Math.Abs(this.Position.Y - ghost.Position.Y) < 12.0f && this.Position.X < ghost.Position.X )
            {
                _plantStatus = PlantStatus.Attack;
              
            }
            if (PlantStatus.Attack == _plantStatus)
            {
               
                for(int i = 0; i<nBullet; i++)
                {
                    if ((!Bullet[i].Visible) && 
                        (Math.Abs(gameTime.TotalGameTime.TotalSeconds % LoadBulletTime) < 0.1) ) // khoi tao gia tri cho vien dan
                    {
                        this.Bullet[i].Visible = true;
                        Vector2 temp;
                        temp.X = this.Position.X + this.Size.X * 0.75f;
                        temp.Y = this.Position.Y + this.Size.Y * 0.05f;
                        this.Bullet[i].Position = temp;
                    }
                    else 
                    {
                        if(ghost.IsHitBullet(this.Bullet[i]))// neu ma bi trung dan
                        {
                            this.Bullet[i].Visible = false;
                        }
                    }
                }
            }           
        }
        public override void Update(GameTime gameTime)
        {
            if (_plantStatus == PlantStatus.Attack)
            {
                for (int i = 0; i < nBullet; i++)
                {
                    Vector2 pos = Bullet[i].Position;
                    pos.X += Velocity;
                    Bullet[i].Position = pos;
                }

            }
            base.Update(gameTime);
        }
     
        public override void Draw(SpriteBatch spriteBatch)
        {
            //base.Draw(spriteBatch);
            switch (_plantStatus)
            {
                case PlantStatus.Normal:
                    {
                        Sprites[0].Position = Position;
                        Sprites[0].Draw(spriteBatch);
                        break;
                    }
                case PlantStatus.Attack:
                    {
                        Sprites[1].Position = Position;
                        Sprites[1].Draw(spriteBatch);
                        for(int i =0;i <nBullet; i++)
                            Bullet[i].Draw(spriteBatch);
                        break;
                    }

            }
        }

        public override void AddBullet(Sprite[] bullet, int n)
        {
            this.Bullet = new Sprite[n];
            for (int i = 0; i < n; i++)
            {
                this.Bullet[i] = bullet[i];
            }
        }
        
    }
}
